package cn.qihua.dao.impl;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import cn.qihua.dao.IBaseOrderDAO;
import cn.qihua.model.BaseOrder;

/**
 * A data access object (DAO) providing persistence and search support for
 * BaseOrder entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.qihua.model.BaseOrder
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class BaseOrderDAO extends BaseHibernateDAO implements IBaseOrderDAO{
	private static final Logger log = LoggerFactory
			.getLogger(BaseOrderDAO.class);
	// property constants
	public static final String NBID = "nbid";
	public static final String TEXT = "text";
	public static final String USER_ID = "userId";
	public static final String TYPE = "type";
	public static final String LINKMAN = "linkman";
	public static final String PHONE = "phone";
	public static final String QQ = "qq";
	public static final String ISNEW = "isnew";
	public static final String STATUS = "status";
	public static final String REMARK = "remark";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#save(cn.qihua.model.BaseOrder)
	 */
	@Override
	public Boolean save(BaseOrder transientInstance) {
		log.debug("saving BaseOrder instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#delete(cn.qihua.model.BaseOrder)
	 */
	@Override
	public void delete(BaseOrder persistentInstance) {
		log.debug("deleting BaseOrder instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findById(java.lang.Integer)
	 */
	@Override
	public BaseOrder findById(java.lang.Integer id) {
		log.debug("getting BaseOrder instance with id: " + id);
		try {
			BaseOrder instance = (BaseOrder) getCurrentSession().get(
					"cn.qihua.model.BaseOrder", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByExample(cn.qihua.model.BaseOrder)
	 */
	@Override
	public List findByExample(BaseOrder instance) {
		log.debug("finding BaseOrder instance by example");
		try {
			List results = getCurrentSession()
					.createCriteria("cn.qihua.model.BaseOrder")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding BaseOrder instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from BaseOrder as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByNbid(java.lang.Object)
	 */
	@Override
	public List findByNbid(Object nbid) {
		return findByProperty(NBID, nbid);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByText(java.lang.Object)
	 */
	@Override
	public List findByText(Object text) {
		return findByProperty(TEXT, text);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByUserId(java.lang.Object)
	 */
	@Override
	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByBaidu(java.lang.Object)
	 */
	@Override
	public List findByBaidu(Object baidu) {
		return findByProperty(BAIDU, baidu);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByType(java.lang.Object)
	 */
	@Override
	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByLinkman(java.lang.Object)
	 */
	@Override
	public List findByLinkman(Object linkman) {
		return findByProperty(LINKMAN, linkman);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByPhone(java.lang.Object)
	 */
	@Override
	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByQq(java.lang.Object)
	 */
	@Override
	public List findByQq(Object qq) {
		return findByProperty(QQ, qq);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByIsnew(java.lang.Object)
	 */
	@Override
	public List findByIsnew(Object isnew) {
		return findByProperty(ISNEW, isnew);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByStatus(java.lang.Object)
	 */
	@Override
	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByRemark(java.lang.Object)
	 */
	@Override
	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all BaseOrder instances");
		try {
			String queryString = "from BaseOrder";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public BaseOrder merge(BaseOrder detachedInstance) {
		log.debug("merging BaseOrder instance");
		try {
			BaseOrder result = (BaseOrder) getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(BaseOrder instance) {
		log.debug("attaching dirty BaseOrder instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(BaseOrder instance) {
		log.debug("attaching clean BaseOrder instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#update(cn.qihua.model.BaseOrder)
	 */
	@Override
	public Boolean update(BaseOrder transientInstance) {
		log.debug("updateing BaseOrder instance");
		try {
			getCurrentSession().update(transientInstance);
			log.debug("update successful");
			return true;
		} catch (RuntimeException re) {
			log.error("update failed", re);
			return false;
		} catch (Exception re) {
			log.error("update failed", re);
			return false;
		}
	}
	
	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByProperty(java.lang.String, java.lang.Object, int, int)
	 */
	@Override
	public List<BaseOrder> findByProperty(String propertyName, Object value,
			int start, int limit) {
		log.debug("finding BaseOrder instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from BaseOrder as model where model."
					+ propertyName + "= ? ORDER BY model.id DESC";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setFirstResult(start);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByStatus(java.lang.Object, int, int)
	 */
	@Override
	public List<BaseOrder> findByStatus(Object status, int start, int limit) {
		return findByProperty(STATUS, status, start, limit);
	}
	
	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#getTotal()
	 */
	@Override
	public Integer getTotal() {
		try {
			String queryString = "select count(*) from BaseOrder";
			Query queryObject = getCurrentSession().createQuery(queryString);

			Number num = (Number) queryObject.list().get(0);
			return num.intValue();
		} catch (RuntimeException re) {
			log.error("find count failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#changeStatus(int, java.lang.String)
	 */
	@Override
	public boolean changeStatus(int id, String status) {
		try {
			String queryString = "UPDATE BaseOrder SET status=? WHERE id=?)";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setString(0, status);
			queryObject.setInteger(1, id);
			if (queryObject.executeUpdate() > 0) {
				return true;
			}
		} catch (RuntimeException re) {
			log.error("UPDATE Customer failed", re);
			throw re;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#getTotalByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public Integer getTotalByProperty(String propertyName, Object value) {
		try {
			String queryString = "select count(*) from BaseOrder where "
					+ propertyName + "= ?";

			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);

			Number num = (Number) queryObject.list().get(0);
			return num.intValue();
		} catch (RuntimeException re) {
			log.error("find count failed", re);
			throw re;
		}
	}
	
	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findAll(int, int)
	 */
	@Override
	public List<BaseOrder> findAll(int start, int limit) {
		log.debug("finding all Customer instances");
		try {
			String queryString = "from BaseOrder";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setFirstResult(start);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	/* (non-Javadoc)
	 * @see cn.qihua.dao.impl.IBaseOrderDAO#findByFuzzy(java.lang.String, java.lang.Object)
	 */
	@Override
	public List<BaseOrder> findByFuzzy(String propertyName, Object value) {
	
		try {
			String queryString = "from BaseOrder as model where model."
					+ propertyName + "LIKE %?%";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public static IBaseOrderDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IBaseOrderDAO) ctx.getBean("BaseOrderDAO");
	}
}